﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Quan_Li_Bai_Viet2 : System.Web.UI.Page
{
    public PagedDataSource pds = new PagedDataSource();
    private int catid;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["TenDangNhap"] == null || Session["MatKhau"] == null)
        {
            Response.Redirect("login.aspx");
        }
        catid = drlCat.SelectedIndex + 1;

        if (!IsPostBack)
        {
            BindGridContent();
        }
        BindGridContent();

    }
    public void BindGridContent()
    {
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["hyundaisaigonConnectionString"].ConnectionString);
        connection.Open();
        String sql;
        if (catid != null)
        {
            sql = "select * from articles where caid = " + catid + " order by id DESC";
        }
        else
        {
            sql = "select * from articles order by id DESC";
        }
        SqlDataAdapter da = new SqlDataAdapter(sql, connection);
        DataTable dt = new DataTable();
        da.Fill(dt);
        connection.Close();
        pds.DataSource = dt.DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = Convert.ToInt16(ddlPageSize.SelectedValue);
        pds.CurrentPageIndex = CurrentPage;
        lnkbtnNext.Enabled = !pds.IsLastPage;
        lnkbtnPrevious.Enabled = !pds.IsFirstPage;
        dlCountry.DataSource = pds;
        dlCountry.DataBind();
        doPaging();

    }


    public int CurrentPage
    {
        get
        {
            if (this.ViewState["CurrentPage"] == null)
                return 0;
            else
                return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
        }
        set
        {
            this.ViewState["CurrentPage"] = value;
        }

    }
    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
        for (int i = 0; i < pds.PageCount; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }

        dlPaging.DataSource = dt;
        dlPaging.DataBind();

    }
    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("lnkbtnPaging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            BindGridContent();
        }
    }
    protected void lnkbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        BindGridContent();
    }

    protected void lnkbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        BindGridContent();
    }

    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        CurrentPage = 0;
        BindGridContent();
    }

    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            lnkbtnPage.Font.Bold = true;
        }
    }
    protected string check(string s)
    {
        if (s.Contains("http://") || s.Contains("www."))
        {
            return Eval("image").ToString();
        }
        else
        {
            return "/" + Eval("image").ToString();
        }

    }
    protected string createURLFromID(string id)
    {
        String url = "Viet-Bai.aspx?id=" + id + "&caid=" + catid;
        return url;
    }
    protected void DangXuat_Click(object sender, EventArgs e)
    {
        Session.RemoveAll();
        Response.Redirect("Default.aspx");
    }

    protected void ThemSp_Click(object sender, EventArgs e)
    {
        Response.Redirect("admin.aspx");
    }
}